<template>
  <div>
    <el-upload
       :show-file-list="false"
       :on-success="handleUploadSuccess"
       :before-upload="beforeUpload"
       :on-progress="uploadVideoProcess"
      :action="dataObj.host"
      :data="dataObj"
      list-type="picture"
      :multiple="false"
      :file-list="fileList"
      :on-remove="handleRemove"
      :on-preview="handlePreview"
    >
    <video style="width:200px" v-if="fileList[0].url!=''" :src="fileList[0].url" class="avatar" controls="controls">您的浏览器不支持视频播放</video>
      <el-progress v-if="videoFlag == true" type="circle" :percentage="videoUploadPercent"></el-progress>
      <div slot="tip" class="el-upload__tip">
           <el-tag type="success" v-if="isVideo(fileList[0].url)">只能上传mp4/avi格式的视频"</el-tag> 
      </div>
    </el-upload>
  </div>
</template>
<script>
import { policy } from "./policy";
import { getUUID } from "@/utils";

export default {
  name: "videoleUpload",
  props: {
    value: String,
  },
  computed: {
    videoUrl() {
      return this.value;
    },
    videoName() {
      if (this.value != null && this.value !== "") {
        return this.value.substr(this.value.lastIndexOf("/") + 1);
      } else {
        return null;
      }
    },
    fileList() {
      return [
        {
          name: this.videoName,
          url: this.videoUrl,
        },
      ];
    },
    showFileList: {
      get: function () {
        return (
          this.value !== null && this.value !== "" && this.value !== undefined
        );
      },
      set: function (newValue) {},
    },
  },
  data() {
    return {
      dataObj: {
        policy: "",
        signature: "",
        key: "",
        ossaccessKeyId: "",
        dir: "",
        host: "",
        // callback:'',
      },
      videoUploadPercent:0,
      percentage:0,
      videoFlag:true,
      fileType:"", //这里定义一个文件类型是判断是否是图片 视频 压缩包 或者文档
      dialogVisible: false,
    };
  },
  methods: {
    isVideo() {
      let fileType = this.getFileType();
      return ~[".mp4"].indexOf(fileType);
    },
    getFileType() {
       this.fileType = this.value.substring(this.value.lastIndexOf("."));
      return this.fileType;
    },
    getUUID() {
      //生成UUID
      return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
        return (
          c === "x" ? (Math.random() * 16) | 0 : "r&0x3" | "0x8"
        ).toString(16);
      });
    },
    emitInput(val) {
      this.$emit("input", val);
    },
    handleRemove(file, fileList) {
      this.emitInput("");
    },
    handlePreview(file) {
      this.dialogVisible = true;
    },
    beforeUpload(file) {
      console.log(file.type);
      if(["video/mp4"].indexOf(file.type)==-1&&["audio/mpeg"].indexOf(file.type)==-1){
         this.$message.error('请上传正确的视频格式');
         this.videoFlag=true;
         this.videoUploadPercent=0;
         return false;
      }  
      let _self = this;
      return new Promise((resolve, reject) => {
        policy()
          .then((response) => {
            _self.dataObj.policy = response.data.policy;
            _self.dataObj.signature = response.data.signature;
            _self.dataObj.ossaccessKeyId = response.data.accessid;
            _self.dataObj.key =
              response.data.dir + this.getUUID() + "_${filename}";
            _self.dataObj.dir = response.data.dir;
            _self.dataObj.host = response.data.host;
            resolve(true);
          })
          .catch((err) => {
            reject(false);
          });
      });
    },
    handleUploadSuccess(res, file) {
      this.showFileList = true;
      this.fileList.pop();
      this.fileList.push({
        name: file.name,
        url:
          this.dataObj.host +
          "/" +
          this.dataObj.key.replace("${filename}", file.name),
      });
      this.emitInput(this.fileList[0].url);
      this.videoFlag=false
    },
    uploadVideoProcess(event, file, fileList){
    this.videoFlag = true;
    this.videoUploadPercent = Number(file.percentage.toFixed(0));
},
  },
};
</script>
<style>
</style>


